<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  admin_activity.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-19.
//  Copyright 2011 lishuzu. All rights reserved.
// 

class Admin_user extends CI_Controller {

	 function __construct()
	 {
		parent::__construct();
 		$this->load->model('common/security_model');
		if($this->security_model->isAdminLogin() === FALSE){
          showmessage('请登录', 'admin/login/index', 3);
		}
 		$this->load->model('admin_user_model');
 		$this->load->model('user_model');
		$this->load->library('user_agent');
	 }

	function index()
	{
		$data = array();
		$data = $this->admin_user_model->get_list();
		$this->load->template('admin/admin_user_tpl', $data);
	}
	
	function manage(){
		$uid = $this->input->get('uid') ? intval($this->input->get('uid', true)) : '';
		if (!$uid) {
			showmessage('未选择用户');
		}
		$data['member'] = $this->user_model->getuser('uid', $uid);
		$data['member']['intro'] = str_replace("<br/>", "\r\n",  $data['member']['intro']);
		$data['usergroup'] = $this->config->item('usergroup');
		if (submitcheck('manageuersubmit')) {
			$name= $this->input->post('name') ? $this->input->post('name', true) : '';
			$intro= $this->input->post('intro') ? $this->input->post('intro', true) : '';
			$myblog= $this->input->post('myblog') ? $this->input->post('myblog', true) : '';
			$status= $this->input->post('status') ? $this->input->post('status', true) : '';
			$namestatus= $this->input->post('namestatus') ? $this->input->post('namestatus', true) : '';
			$passreason= $this->input->post('passreason') ? $this->input->post('passreason', true) : '';
			
			$password = $this->input->post('password', true) ? $this->input->post('password', true) : '';
			$groupid = $this->input->post('groupid', true) ? $this->input->post('groupid', true) : '';
			$invitedtime = $this->input->post('invitedtime', true) ? $this->input->post('invitedtime', true) : '';
			$refer = $this->input->post('refer');
			
			$updatedata = array(
				'name' => trim($name),
				'intro' => $intro,
				'myblog' => $myblog,
				'status' => intval($status),
				'namestatus' => intval($namestatus),
				'passreason' => $passreason,
				'invitedtime' => intval($invitedtime),
				'groupid' => intval($groupid),
				);
			if ($password) {
				//密码随机hash
				$updatedata['salt'] = substr(uniqid(rand()), -6);
				$updatedata['password']= md5(md5($password).$updatedata['salt']);
			}
			//取消html
			$updatedata = shtmlspecialchars($updatedata);
			$this->user_model->update($uid, $updatedata);
			if (strpos($refer, 'deleteavatar') || !$refer) {
				$refer = base_url().'admin/admin_user/manage/?uid='.$uid;
			}
			showmessage('更新成功', $refer, 3);
		}
		$this->load->template('admin/admin_manageuser_tpl', $data);
	}
	
	// 删除头像
	function deleteavatar(){
		$uid = $this->input->get('uid') ? intval($this->input->get('uid', true)) : '';
		if (!$uid) {
			showmessage('未选择用户');
		}
		$this->user_model->delete_avatar($uid);
		showmessage('删除成功', 'admin/admin_user/manage/?uid='.$uid, 3);
	}
	
	// 批量处理
	function batchop(){
		$uids = $this->input->post('uids', true);
		$optype = intval($this->input->post('optype', true));
		$refer = $this->input->post('refer') ? $this->input->post('refer') : 'admin/admin_user/index';
		if (!$uids) {
			showmessage('未选择用户', $refer, 2);
		}
		if (!$optype) {
			showmessage('未选择操作', $refer, 2);
		}
		switch ($optype) {
			case '1':
				$data =  array('namestatus' => '1');
				$this->user_model->updates($uids,$data);
				break;
			case '2':
				$data =  array('namestatus' => '0');
				$this->user_model->updates($uids,$data);
				break;
			case '3':
				$data =  array('status' => '1');
				$this->user_model->updates($uids,$data);
				break;
			case '4':
				$data =  array('status' => '0');
				$this->user_model->updates($uids,$data);
				break;
		}
		showmessage('操作成功', $refer, 2);
	}
}
?>